home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1369 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  75 lines

  1. Newsgroups: comp.lang.c
  2. Path: gail.ripco.com!mambuhl
  3. From: mambuhl@ripco.com (Martin Ambuhl)
  4. Subject: Re: Splitting String ?   
  5. X-Nntp-Posting-Host: foley.ripco.com
  6. Message-ID: <DL3u9M.9xt@rci.ripco.com>
  7. Sender: usenet@rci.ripco.com (Net News Admin)
  8. Organization: Ripco Internet BBS Chicago
  9. Date: Sat, 13 Jan 1996 05:36:58 GMT
  10. X-Ident-Sender: mambuhl
  11.  
  12. hakola@cadmail.hut.fi (Petri Hakola) in
  13. <HAKOLA.96Jan12151128@jung.hut.fi> asks:
  14.  
  15.  
  16. >        Have I missed something (again:) or why doesn't this code
  17. >        work? I should split dos-a-like-filename and add new postfix
  18. >        instead of old one (i.e. DATA.TXT --> DATA.UPD  It seems to
  19. >        work correctly if the filename has an old postfix, but if
  20. >        there isn't one start won't return what it should.
  21.  
  22. >                                                        - P -
  23.  
  24. I may have missed something here, too.  Your code [at EOM] seems to work
  25. for me.  However, it looks like you do a lot of extra work that may be
  26. obscuring the problem.  Why not start with a replacement for newname
  27. that looks like the following and see if it satisfies your needs:
  28.  
  29. char *newname(char *s)
  30. {
  31.  
  32.     char *dot;
  33.     dot = strchr(s, '.');
  34.     if (dot)
  35.         *dot = '\0';
  36.     strcat(s, ".UPD");
  37.     return s;
  38. }
  39.  
  40.  
  41. [ === Petri's code === ]
  42. >---Clip---
  43. >#include <stdio.h>
  44. >#include <string.h>
  45.  
  46. >char *newname(char *s) {
  47.  
  48. >   char *dot;
  49. >   char *start;
  50. >   char end[5];
  51.  
  52. >   strcpy(end,".UPD");
  53. >   start = s;
  54. >   dot = strchr(s,'.');
  55. >   if( dot == NULL) {
  56. >     start[strlen(start)] = '\0';
  57. >     dot = end;
  58. >   } else {
  59. >   *dot = '\0';
  60. >   dot++;
  61. >   dot = end;
  62. >   }
  63. >   strcat(start, dot);
  64. >   return start;
  65. >}
  66.  
  67. >main() {
  68. >  printf("%s\n",newname("LONGNAME.TXT"));
  69. >  printf("%s\n",newname("NOEND"));
  70. >}
  71.           
  72. --
  73. * Martin Ambuhl       net: mambuhl@ripco.com
  74. * Chicago, IL (USA)    
  75.